package de.jmda.fx.sandbox;

import java.util.LinkedList;
import java.util.List;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import com.sun.javafx.collections.ObservableListWrapper;

public abstract class Sandbox
    extends Application
{
	/**
	 * constructor injection
	 */
	protected Stage stage;

	/**
	 * will be populated with {@link #root} which in turn will be populated with
	 * {@link #group}
	 */
	protected Scene scene;
	
	/**
	 * will be assigned to {@link #scene} and populated with {@link #group}
	 */
	protected Pane root;

	/**
	 * will be populated with arbitrary ui elements
	 */
	protected Group group;

	protected int sceneWidth = 640;

	protected int sceneHeigth = 480;

	@Override
	public void start(Stage stage) throws Exception
	{
		this.stage = stage;
		
		root = new Pane();

		scene = new Scene(root, sceneWidth, sceneHeigth);
		scene.getStylesheets().addAll(getStyleSheets());

		root.getStyleClass().add("root");
		root.getChildren().add(getGroup());

		stage.setTitle(getTitle());
		stage.setScene(scene);
		stage.show();

		populateGroup();
	}

	protected Group getGroup()
	{
		if (group == null)
		{
			group = new Group();
		}

		return group;
	}

	protected abstract void populateGroup();
	
	protected String getTitle()
	{
		return getClass().getSimpleName();
	}

	protected ObservableList<String> getStyleSheets()
	{
		List<String> result = new LinkedList<String>();

		result.add("jmda.mview.css");

		return new ObservableListWrapper<String>(result);
	}
}